#!/bin/bash

################################################################################
##                                                                            ##
## Copyright (c) 2008 FUJITSU LIMITED                                         ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
## Author: Li Zefan <lizf@cn.fujitsu.com>                                     ##
##                                                                            ##
################################################################################

NUM_EVENTS=1
EVENT_TEST_CASES=( "fork" "exec" "exit" "uid" "gid" )

cd $LTPROOT/testcases/bin

export TCID="pec01"
export TST_TOTAL=5
export TST_COUNT=1

exit_status=0

if [ $(id -u) != 0 ]; then
	tst_brkm TCONF ignored "Test must be run as root"
	exit 0;
fi

grep cn_proc /proc/net/connector > /dev/null
if [ $? -ne 0 ]; then
	tst_brkm TCONF ignored "Process Event Connector is not supported or kernel is below 2.6.26"
	exit 0;
fi

# Run a test case
#
# $1: the test number
# $2: type of event
run_case()
{
	export TST_COUNT=$1

	log="$LTPROOT/output/log"
	mkdir -p $log 2> /dev/null

	pec_listener > "$log/listener_$1.log" 2>&1 &
	pid=$!
	# Wait for pec_listener to start listening
	sleep $((1*NUM_EVENTS))

	event_generator -e $2 > "$log/generator_$1.log"
	ret1=$?

	# Sleep until pec_listener has seen and handled all of
	# the generated events
	sleep $((1*NUM_EVENTS))
	kill -s SIGINT $pid 2> /dev/null
	wait $pid
	ret2=$?

	if [ $ret1 -ne 0 -o ! -s "$log/generator_$1.log" ]; then
		tst_resm TFAIL "failed to generate process events"
		exit_status=1
		return 1
	fi

	if [ $ret2 -eq 2 ]; then
		tst_brkm TCONF NULL "connector may not be supported"
		exit 0
	fi

	if [ $ret2 -ne 0 ]; then
		tst_resm TFAIL "failed to listen process events"
		exit_status=1
		return 1
	fi

	event="`cat $log/generator_$1.log`"
	cat "$log/listener_$1.log" | grep "$event" > /dev/null
	if [ $? -eq 0 ]; then
		tst_resm TPASS "get event - $event"
	else
		tst_resm TFAIL "expected event - $event"
		exit_status=1
	fi
}

i=1;
for CASE in "${EVENT_TEST_CASES[@]}" ; do
	run_case $i $CASE
	((i++))
done

exit $exit_status

